package com.sky.controller.user;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.service.UserService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("userOrderController")
@Api(tags = "用户下单相关接口")
@RequestMapping("/user/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    /**
     * 用户下单
     *
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单功能")
    public Result<OrderSubmitVO> sumbit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        // 1.接收参数
        log.info("接收到参数:{}", ordersSubmitDTO);
        // 2.调用service层
        OrderSubmitVO orderSubmitVO = orderService.sumbit(ordersSubmitDTO);
        // 3.响应数据给前端
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
//        log.info("订单支付：{}", ordersPaymentDTO);
//        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
//        log.info("生成预支付交易单：{}", orderPaymentVO);
        orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
        return Result.success();
    }

    /**
     * 用户端查询历史订单
     *
     * @return
     */
    @GetMapping("/historyOrders")
    @ApiOperation("用户端查询历史订单")
    public Result<PageResult> page(Integer page, Integer pageSize, Integer status) {
        // 1.接参
        log.info("接收到参数:{},{},{}", page, pageSize, status);
        // 2.调用
        PageResult pageResult = orderService.page(page, pageSize, status);
        // 3.返回
        return Result.success(pageResult);
    }

    /**
     * 用户查询订单详细信息
     *
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("用户查询订单详细信息")
    public Result<OrderVO> getOrderById(@PathVariable Long id) {
        // 1.接参
        log.info("接收到参数:{}", id);
        // 2.调用
        OrderVO orderVO = orderService.getOrderById(id);
        // 3.响应
        return Result.success(orderVO);
    }

    /**
     * 用户端 取消订单接口
     *
     * @param id
     * @return
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancel(@PathVariable Long id) {
        // 1.接参
        log.info("接收到参数:{}", id);
        // 2.调用
        orderService.cancel(id);
        // 3.响应
        return Result.success();
    }

    /**
     * 用户端 再来一单接口
     *
     * @return
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("用户再来一单接口")
    public Result repetition(@PathVariable Long id) {
        // 1.接参
        log.info("接收到参数,{}", id);
        // 2.调用
        orderService.repetition(id);
        // 3.响应
        return Result.success();
    }


}
